package com.example.supplierfinancin.controller;

import com.example.supplierfinancin.Receipt;
import com.example.util.ContractTools;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigInteger;
import java.util.List;

@RestController
@RequestMapping("receipt")
public class ReceiptController {
    @PostMapping("show_all_in_receipt")
    public String show_all_in_receipt(String privatekey){

        /*
        return:
            {
                status:
                data:[
                        {
                            debtor:2315...
                            debtee:135...
                            receipt_id:12
                            amount:10000
                            deadline:20210130
                        },
                        ...
                    ]
            }
         */
        List<Receipt> temp=ContractTools.get_all_in_receipt(privatekey);
        Gson tool=new Gson();
        JsonObject ret=new JsonObject();
        if(temp==null){
            ret.addProperty("status",false);
            ret.add("data",null);
        }else{
            ret.addProperty("status",true);
            ret.addProperty("data",tool.toJson(temp));
        }
        return ret.toString();
    }
    @PostMapping("show_all_out_receipt")
    public String show_all_out_receipt(String privatekey){
        /*
        return:
            {
                status:
                data:[
                        {
                            debtor:2315...
                            debtee:135...
                            receipt_id:12
                            amount:10000
                            deadline:20210130
                        },
                        ...
                    ]
            }
         */
        List<Receipt> temp=ContractTools.get_all_out_receipt(privatekey);
        Gson tool=new Gson();
        JsonObject ret=new JsonObject();
        if(temp==null){
            ret.addProperty("status",false);
            ret.add("data",null);
        }else{
            ret.addProperty("status",true);
            ret.addProperty("data",tool.toJson(temp));
        }
        return ret.toString();
    }
    @PostMapping("transfer_receipt")
    public String transfer_receipt(String privatekey,String debtee, BigInteger amount, BigInteger deadline){
        /*
        return:
            {
                status:true
            }
         */

        Boolean temp= ContractTools.transfer_receipt(privatekey,debtee,amount,deadline);
        JsonObject ret=new JsonObject();
        ret.addProperty("status",temp);
        return ret.toString();

    }
    @PostMapping("return_receipt")
    public String return_receipt(String privatekey,BigInteger receipt_id,BigInteger amount){
        /*
        return:
            {
                status:true
            }
         */
        Boolean temp = ContractTools.return_receipt(privatekey,receipt_id,amount);
        JsonObject ret=new JsonObject();
        ret.addProperty("status",temp);
        return ret.toString();

    }
}
